package com.lekang.le.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.le.domain.LeaveRequest;
import com.lekang.le.service.ILeaveRequestService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 我的请假Controller
 * 
 * @author szh
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/le/request")
public class LeaveRequestController extends BaseController
{
    @Autowired
    private ILeaveRequestService leaveRequestService;

    /**
     * 查询我的请假列表
     */
    @PreAuthorize("@ss.hasPermi('le:request:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveRequest leaveRequest)
    {
        startPage();
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList(leaveRequest);
        return getDataTable(list);
    }

    /**
     * 导出我的请假列表
     */
    @PreAuthorize("@ss.hasPermi('le:request:export')")
    @Log(title = "我的请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveRequest leaveRequest)
    {
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList(leaveRequest);
        ExcelUtil<LeaveRequest> util = new ExcelUtil<LeaveRequest>(LeaveRequest.class);
        util.exportExcel(response, list, "我的请假数据");
    }
    /**
     * 检查老人是否处于请假中状态
     */
    @PreAuthorize("@ss.hasPermi('le:request:query')")
    @GetMapping("/checkLeaveStatus/{elderlyId}")
    public AjaxResult checkElderlyLeaveStatus(@PathVariable("elderlyId") Long elderlyId)
    {
        try {
            boolean isOnLeave = leaveRequestService.checkElderlyLeaveStatus(elderlyId);
            return AjaxResult.success(isOnLeave);
        } catch (Exception e) {
            logger.error("检查老人请假状态失败", e);
            return AjaxResult.error("检查老人请假状态失败");
        }
    }

    /**
     * 根据老人ID和请假状态查询请假记录
     */
    @PreAuthorize("@ss.hasPermi('le:request:query')")
    @GetMapping("/listByElderlyAndStatus")
    public AjaxResult listByElderlyAndStatus(@RequestParam Long elderlyId, @RequestParam String leaveStatus)
    {
        try {
            List<LeaveRequest> list = leaveRequestService.selectByElderlyIdAndStatus(elderlyId, leaveStatus);
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("查询老人请假记录失败", e);
            return AjaxResult.error("查询老人请假记录失败");
        }
    }
    /**
     * 获取我的请假详细信息
     */
    @PreAuthorize("@ss.hasPermi('le:request:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveRequestService.selectLeaveRequestById(id));
    }

    /**
     * 新增我的请假
     */
    @PreAuthorize("@ss.hasPermi('le:request:add')")
    @Log(title = "我的请假", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveRequest leaveRequest)
    {
        return toAjax(leaveRequestService.insertLeaveRequest(leaveRequest));
    }

    /**
     * 修改我的请假
     */
    @PreAuthorize("@ss.hasPermi('le:request:edit')")
    @Log(title = "我的请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveRequest leaveRequest)
    {
        return toAjax(leaveRequestService.updateLeaveRequest(leaveRequest));
    }

    /**
     * 删除我的请假
     */
    @PreAuthorize("@ss.hasPermi('le:request:remove')")
    @Log(title = "我的请假", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveRequestService.deleteLeaveRequestByIds(ids));
    }
}
